package com.wind.exp;

import com.wind.vo.ResultData;
import com.wind.vo.ResultDataGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * 统一异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

//    @ExceptionHandler(CommonException.class)
//    @ResponseBody
//    public CommonResultRsp handleCommonException(CommonException e) {
//        CommonResultRsp rsp = new CommonResultRsp();
//        rsp.setStatus(TradeState.FAIL);
//        rsp.setMessage(e.getMessage());
//        if(StringUtils.isEmpty(e.getMessage())){
//            rsp.setMessage(ExceptionCode.getMessage(e.getErrorCode()));
//        }
//        return rsp;
//    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultData handleExceptoin(Exception e){
        logger.error("发生未知异常:",e);
        return ResultDataGenerator.genFailResult(null,e.getMessage());
    }
}
